#include "CoderToJpg.h"

int getFileLen()
{
	FILE* pFile;
	int size;
	pFile = fopen("..\\testvideo\\one.yuv", "rb");
	if (pFile==NULL)
	{
		perror("Error opening file");
		return 0;
	}
	else
	{
       fseek(pFile, 0, SEEK_END);
       size = ftell(pFile);
       fclose(pFile);
	   return size;
    }
	return 0;
}

int readFromFile(AVFrame* p_pobjAVFrame)
{
	int nLen = getFileLen();
	if (nLen <= 0)
	{
		return -1;
	}

	char* szData = new char[nLen];
	memset(szData, 0, nLen);

	FILE* fp = fopen("..\\testvideo\\one.yuv", "rb");
	if (fp == NULL)
	{
		printf("fopen err\n");
		return -1;
	}

	int nReadLen = fread(szData, 1, nLen, fp);
	if (nReadLen <= 0)
	{
		printf("fread err\n");
		return -1;
	}

	p_pobjAVFrame->data[0] = (uint8_t *)szData;
	p_pobjAVFrame->linesize[0] = 2624;

	//p_pobjAVFrame->data[1] = ;
	//p_pobjAVFrame->linesize[1] = ;

	//p_pobjAVFrame->data[2] = ;
	//p_pobjAVFrame->linesize[2] = ;
	return 0;
}

int Coder_Test()
{
	CoderToJpg objCoderToJpg;
	int nRet = -1;
	nRet = objCoderToJpg.init();
	if (nRet != 0)
	{
		printf("H264FileParser open err %d\n", nRet);
		return -1;
	}
	objCoderToJpg.setWidth(2592);
	objCoderToJpg.setHeight(1520);

	AVFrame objAVFrame;
	string strJpgFileName = "test.jpg";

	nRet = readFromFile(&objAVFrame);
	if (nRet != 0)
	{
		printf("readFromFile err %d\n", nRet);
		return -1;
	}

	nRet = objCoderToJpg.coderFrameToPic(&objAVFrame, strJpgFileName);
	if (nRet != 0)
	{
		printf("H264FileParser open err %d\n", nRet);
		return -1;
	}
	delete[] objAVFrame.data[0];
	return 0;
}
